package com.example.tractorframework.helloworld;

import android.graphics.PointF;
import android.view.MotionEvent;

import com.tractorframework.core.receptor.IOperation;
import com.tractorframework.core.receptor.OperationBase;

public class ScrollOperation extends OperationBase {

	private PointF first;
	
	private PointF last;
	
	@Override
	protected boolean accept(Object source, Object event) {
		MotionEvent motion = (MotionEvent)event;
		PointF current = new PointF(motion.getX(), motion.getY());
		if(motion.getAction() == MotionEvent.ACTION_DOWN)
		{
			first = current;
			last = null;
		}
		float length  = PointF.length(current.x - first.x, current.y - first.y);
		float xRatio  = current.y == first.y ? Float.MAX_VALUE : Math.abs((current.x - first.x ) / (current.y - first.y));
		return length >= 20 && xRatio > 1;
	}

	@Override
	protected boolean continueAcceptCheck(Object target, Object event) {
		MotionEvent motion = (MotionEvent)event;
		return motion.getAction() != MotionEvent.ACTION_UP;
	}

	@Override
	protected void operate(Object source, Object event) {
		MotionEvent motion = (MotionEvent)event;
		
		PointF current = new PointF(motion.getX(), motion.getY());
		if(last != null)
		{
			fireOperation(Events.Scroll, source, (current.x - last.x));
		}
		last = current;
		if(motion.getAction() == MotionEvent.ACTION_UP)
		{
			this.close();
		}
	}

	@Override
	protected void onOpen() {
	}

	@Override
	protected void onClose() {
		
	}



}
